package com.ssyt.yt1.day17;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * TreeSet：自定义对象的排序
 * 
 * @author Administrator
 *
 */
class Student implements Comparable<Student> {

	private String name;
	private int age;
	private int score;

	public Student(String name, int age, int score) {
		super();
		this.name = name;
		this.age = age;
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", score=" + score
				+ "]";
	}

	@Override
	public int compareTo(Student o) {
		if (this.age > o.age) {
			return 1;
		} else if (this.age < o.age) {
			return -1;
		} else {
			if (this.score > o.score) {
				return 1;
			} else if (this.score < o.score) {
				return -1;
			} else {
				return this.name.compareTo(o.name);
			}
		}
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public int getScore() {
		return score;
	}

}

public class TreeSetDemo2 {

	public static void main(String[] args) {
		/**
		 * 利用Comparator实现外部排序解决内部排序的不足！
		 */
		Set<Student> studentSet = new TreeSet<>(new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				if (o1.getAge() > o2.getAge()) {
					return -1;
				} else if (o1.getAge() < o2.getAge()) {
					return 1;
				} else {
					if (o1.getScore() > o2.getScore()) {
						return 1;
					} else if (o1.getScore() < o2.getScore()) {
						return -1;
					} else {
						return o1.getName().compareTo(o2.getName());
					}
				}
			}

		});
		studentSet.add(new Student("张三", 18, 90));
		studentSet.add(new Student("李四", 18, 60));
		studentSet.add(new Student("王五", 20, 91));
		studentSet.add(new Student("赵六", 20, 91));
		for (Student student : studentSet) {
			System.out.println(student);
		}

	}
}
